package com.cdshangpai.utils.db;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.cdshangpai.utils.db.beans.Form;

import java.util.ArrayList;

public class FormDb {
    private static SQLiteDatabase db=DatabaseHelper.getDb();
    private static String tablename="t_form";

    public Long insert(Form form)
    {
        Long id=db.insert(tablename,null,form.getValues());
        return id;
    }

    public void update(Form form)
    {
        db.update(tablename,form.getValues(),"id=?",new String[]{form.id.toString()});
    }

    public void delete(Integer id)
    {
        db.delete(tablename,"id=?",new String[]{id.toString()});
    }

    public void deleteByIds(Integer[] ids)
    {
        StringBuilder where=new StringBuilder();
        ArrayList<String> wheres=new ArrayList<>();
        for(int i=0;i<ids.length;i++)
        {
            where.append("?");
            if(i!=ids.length-1)
            {
                where.append(",");
            }
            wheres.add(ids[i].toString());
        }
        String sql=" id in("+where.toString()+")";
        db.delete(tablename,sql,wheres.toArray(new String[wheres.size()]));
    }

    public void clean()
    {
        String sql="delete from "+tablename;
        db.execSQL(sql);
    }

    public ArrayList<Form> queryByAccountId(Integer accountId)
    {
        ArrayList<Form> result=new ArrayList<>();
        String sql="select * from "+tablename+" where accountid="+accountId;
        Cursor c=db.rawQuery(sql,null);
        c.moveToFirst();
        while (!c.isAfterLast())
        {
            Form form=cToForm(c);
            result.add(form);
            c.moveToNext();
        }
        c.close();
        return result;
    }

    public ArrayList<Form> query()
    {
        ArrayList<Form> result=new ArrayList<>();
        String sql="select * from "+tablename;
        Cursor c=db.rawQuery(sql,null);
        c.moveToFirst();
        while (!c.isAfterLast())
        {
            Form form=cToForm(c);
            result.add(form);
            c.moveToNext();
        }
        c.close();
        return result;
    }

    private Form cToForm(Cursor c)
    {
        Form form=new Form();
        form.id=c.getInt(c.getColumnIndex("id"));
        form.accountid=c.getInt(c.getColumnIndex("accountid"));
        form.name=c.getString(c.getColumnIndex("name"));
        form.phone=c.getString(c.getColumnIndex("phone"));
        form.remark=c.getString(c.getColumnIndex("remark"));
        form.caseids=c.getString(c.getColumnIndex("caseids"));
        form.videoids=c.getString(c.getColumnIndex("videoids"));
        return form;
    }

    public void test()
    {
        clean();
        Form form=new Form();
        form.name="name";
        form.phone="phone";
        form.remark="remark";
        form.accountid=1;
        form.caseids="caseids";

        Long id=insert(form);

        ArrayList<Form> forms=queryByAccountId(1);

        forms=query();

        form.id=id.intValue();
        form.name="name update";
        update(form);

        forms=query();

        delete(id.intValue());
        forms=query();

        id=insert(form);
        Integer[] ids=new Integer[]{id.intValue()};
        deleteByIds(ids);

        forms=query();


    }
}
